<template>
	<view>
		<view style="width:100%;background: #fff;">
			<u-tabs :list="tabList" @click="click" :current="current" :scrollable="false" lineColor="#3c9cff">
			</u-tabs>
		</view>

		<view v-if="dataList && dataList.length>0">

			<view class="order" v-for="(item,index) in dataList" :key="index">
				<view class="order-number">
					<view class="number-left">
						<image src="../../static/kuaidi/number.png" mode="widthFix"></image>
						<text>{{ item.orderNumber }}</text>
					</view>
					<view class="number-right"
						:class="item.status == 'COMPLETED' ? 'green':item.status == 'PICKED_UP'?'blue':'red'">
						{{ getStatus(item.status) }}
						<text style="color: red;">-{{isState(item.goodStatus)}}</text>
					</view>
				</view>
				<!-- 	<view class="order-item">
					<image src="../../static/kuaidi/title.png" mode="widthFix"></image>
					<text style="font-size: 30rpx; color: #000000;" class="row1">{{ item.title }}</text>
				</view> -->
				<view class="order-item">
					<view>
						<image src="../../static/kuaidi/addrer.png" mode="widthFix"></image>
						<text>装货地：</text>
						<text>{{ item.deliveryStreet+item.deliveryAddress }}</text>
					</view>
				</view>
				<view class="order-item">
					<image src="../../static/kuaidi/maddres.png" mode="widthFix"></image>
					<text>卸货地：</text>
					<text>{{ item.pickupStreet+item.pickupAddress }}</text>
				</view>
				<view class="order-item">
					<image src="../../static/kuaidi/time.png" mode="widthFix"></image>
					<text>用车时间：</text>
					<text>{{ $uti.dateFormat(item.appointmentTime,'yyyy-MM-dd hh:mm:ss') }}</text>
				</view>
				<view class="order-bom">
					<!-- <view class="order-bom-left">装货地：{{ item.disance+'km' }}</view> -->
					<view class="order-bom-right" v-if="item.goodStatus!='DISCHARGED'">

						<view class="box1" @click="jieOrderReceiving(item)" v-if="item.goodStatus=='FREIGHT'">已装货</view>
						<view class="box2" @click="daoHangView(1,item)" v-if="item.goodStatus=='FREIGHT'">去装货</view>


						<view class="box2" style="background-color: #e5a84a;" @click="jieOrderOut(item)"
							v-if="item.goodStatus=='UNLOADED'">已卸货</view>
						<view class="box2" @click="daoHangView(2,item)" v-if="item.goodStatus=='UNLOADED'">去卸货</view>

						<view class="box3" @click="telPhone(item.deliveryPhone)">联系装货</view>
						<view class="box3" style="background-color: #fc51f4;" @click="telPhone(item.pickPhone)">联系卸货
						</view>
					</view>
					<view v-else>
						当前订单已完成卸货，等待订单结算中~
					</view>

				</view>

				<view class="order-price">

					<text>{{item.price}}</text>
					<text style="font-size: 30rpx;align-items: center;">￥</text>
				</view>
			</view>
			<u-loadmore :status="status" />
			<image src="/static/service.png"
				style="width:120rpx;height: 150rpx;position: fixed;bottom: 50rpx;left:20rpx" @click="makePhone"></image>
		</view>

		<view v-else style="margin-top: 200rpx;">
			<u-empty mode="order">
			</u-empty>
		</view>


		<!-- <u-popup mode="bottom" :show="!isLogin" round="10">
			<login @success="loginClose"></login>
		</u-popup> -->
	</view>
</template>

<script>
	import login from '../../components/login/login.vue'
	export default {
		components: {
			login
		},
		data() {
			return {
				isLogin: false,
				tabList: [{
						name: '全部',
						state: 'ALL'
					},
					{
						name: '进行中',
						state: 'PICKED_UP'
					},
					{
						name: '已完成',
						state: 'COMPLETED'
					},
					{
						name: '已取消',
						state: 'CANCELLED'
					}
				],
				current: 0,
				isload: true,
				page: 1,
				rows: 10,
				dataList: [],
				status: 'loading'
			}
		},
		onLoad() {
			var that = this;
			that.isLogin = that.$us.checkLogin();
			if (that.isLogin) {
				that.getList();
			}
			that.getList();
			console.log('是否登陆', that.isLogin)
		},
		onShow() {
			var that = this;
			that.isLogin = that.$us.checkLogin();
		},
		methods: {
			difference(item) {
				var that = this;
				that.$http.post('Order/PayOrderDifference', {
					userGuid: that.$us.getId(),
					orId: item.id
				}, function(res) {
					if (res.StatusCode == 1) {
						that.weChatPay(res.Data);
					} else {
						that.$uti.alert(res.Message);
					}
				}, '加载中...')
			},

			telPhone(phoneNumber) {
				uni.makePhoneCall({
					phoneNumber: phoneNumber,
					fail() {
						console.log("取消拨打");
					},
				});
				// uni.openLocation({
				// 	latitude: Number(this.location.latitude),
				// 	longitude: Number(this.location.longitude),
				// 	name: "测试" || ''
				// });

			},

			jieOrderReceiving(item) {
				let that = this;
				this.$uti.confirm("请确认已与货主核对并且已完成装货！", function() {

					that.updateOrderStstus(item);


				})

			},
			jieOrderOut(item) {
				let that = this;
				this.$uti.confirm("请确认已与货主核对并且已完成卸货！", function() {

					that.updateOrderOutStstus(item);


				})

			},
			daoHangView(e, item) {
				let that = this;
				// console.log(that.orderInfo, "that.orderInfo")
				if (e == 1) {

					uni.openLocation({
						latitude: Number(item.deAdLatitude), // 目标纬度
						longitude: Number(item.deAdLongitude), // 目标经度
						// name: e.hardwareName,
						// address: e.hardwareAddress,
						success: function() {
							console.log('打开地图导航成功');
						},
						fail: function(err) {
							console.log('打开地图导航失败', err);
						}
					});

				} else {

					uni.openLocation({
						latitude: Number(item.pDLatitude), // 目标纬度
						longitude: Number(item.pDLongitudee), // 目标经度
						// name: e.hardwareName,
						// address: e.hardwareAddress,
						success: function() {
							console.log('打开地图导航成功');
						},
						fail: function(err) {
							console.log('打开地图导航失败', err);
						}
					});

				}

			},
			//已经卸货完成

			updateOrderOutStstus(item) {
				let that = this;
				that.$http.post('order/updateGoods', {
					// userGuid: that.$us.getId(),
					// orId: item.id
					orderId: item.id,
					goodStatus: 'DISCHARGED',
					// driveId: '17',


				}, function(res) {
					if (res.data.code == 200) {
						that.$uti.alert("已成功装货！");

						that.reload();
					} else {
						that.$uti.alert(res.data.msg);
					}

				}, '加载中...')
			},
			//已经装货
			updateOrderStstus(item) {
				let that = this;
				that.$http.post('order/updateGoods', {
					// userGuid: that.$us.getId(),
					// orId: item.id
					orderId: item.id,
					goodStatus: 'UNLOADED',
					// driveId: '17',


				}, function(res) {
					if (res.data.code == 200) {
						that.$uti.alert("已成功装货！");

						that.reload();
					} else {
						that.$uti.alert(res.data.msg);
					}

				}, '加载中...')
			},

			weChatPay(data) {
				var that = this;
				uni.requestPayment({
					provider: 'wxpay',
					nonceStr: data.nonceStr,
					package: data.package,
					paySign: data.paySign,
					signType: data.signType,
					timeStamp: data.timeStamp,
					success: (res) => {
						console.log('success:' + JSON.stringify(res));
						uni.showToast({
							title: '支付成功！',
							duration: 2000
						});
						setTimeout(function() {
							that.reload();
						}, 1000)
					},
					fail: (err) => {
						console.log('fail:' + JSON.stringify(err));
						uni.showToast({
							title: '已取消支付！',
							icon: 'none',
							duration: 2000
						});
					}
				});
			},
			traces(item) {
				var that = this;
				if (!item.orTraces) {
					that.$uti.alert('没有物流信息');
					return;
				}

				var json = JSON.parse(item.orTraces);
				uni.setStorageSync('orTraces', json);
				that.$uti.toPage('/pages/order/logistics', {})
				console.log('物流信息', json)
			},
			cancelOrder(item) {
				var that = this;
				that.$uti.confirm('您确定取消订单吗？', function(res) {
					that.$http.post('Order/CancelOrder', {
						userGuid: that.$us.getId(),
						orId: item.id
					}, function(res) {
						that.$uti.alert(res.Message);
						if (res.StatusCode == 1) {
							that.reload();
						}
					}, '取消中...')
				})
			},
			click(e) {
				console.log(e);
				this.current = e.index;
				this.reload();
			},
			loginClose() {
				var that = this;
				that.isLogin = that.$us.checkLogin();
				if (that.isLogin) {
					that.getList();
				}
			},
			reload() {
				this.dataList = [];
				this.page = 1;
				this.isload = true;
				this.getList();
			},
			getList() {
				var that = this;
				if (!that.isload) {
					that.$uti.alert('没有更多数据了');
					return;
				}
				that.status = 'loading';

				that.$http.get('order', {
					// userGuid: that.$us.getId(),
					orderStatus: that.tabList[that.current].state,
					driveId: '17',

					page: that.page,
					size: that.rows
				}, function(res) {
					if (res.data.code == 200) {
						if (that.page * 10 < res.data.data.total) {
							that.status = 'loadmore';
							that.page++;
						} else {
							that.isload = false;
							that.status = 'nomore';
						}

						that.dataList = res.data.data.list

						// res.Data.Result.forEach(item => {
						// 	item.statusText = '';
						// 	item.statusText = that.getStatus(item.state);

						// 	var name = that.$cfg.logList.find(_ => _.key == item.orShipperCode);
						// 	if (name && name.name)
						// 		item.orShipperName = name.name;

						// 	that.dataList.push(item);
						// })
						console.log('订单列表', that.dataList)
					} else {
						that.$uti.alert(res.Message);
						that.isload = false;
						that.status = 'nomore';
					}
				})
			},
			isState(state) {
				if (state == 'FREIGHT') {
					return '待装货'
				} else if (state == 'UNLOADED') {
					return '待卸货'
				} else {
					return '已卸货'
				}
			},

			getStatus(text) {
				var status = '';
				switch (text.toString()) {
					case 'CREATED':
						status = '新增'
						break;
					case '-2':
						status = '支付失败'
						break;
					case 'CANCELLED':
						status = '已取消'
						break;
					case '99':
						status = '调度异常'
						break;
					case 'PICKED_UP':
						status = '进行中'
						break;
					case '102':
						status = '分配网点'
						break;
					case '103':
						status = '分配快递员'
						break;
					case 'ONGOING':
						status = '进行中'
						break;
					case '301':
						status = '待揽件'
						break;
					case '2':
						status = '运输中'
						break;
					case 'COMPLETED':
						status = '已完成'
						break;
					default:
						status = '未知状态'
						break;
				}
				return status;
			}
		},
		onPullDownRefresh() {
			this.reload();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000)
		},
		onReachBottom() {
			this.getList();
		}
	}
</script>

<style lang="scss">
	.template-view {
		width: 90%;
		margin: 0 auto;
		border-radius: 20rpx;
		background: #fff;
		padding: 20rpx;
		margin-top: 20rpx;
	}

	.address-city {
		font-weight: bold;
		font-size: 32rpx;
		text-align: center;
	}

	.address-name {
		font-size: 26rpx;
		color: #999;
		text-align: center;
	}

	.font-def {
		font-size: 26rpx;
		color: #999;
	}

	.green {
		color: green;
	}

	.red {
		color: red;
	}

	.blue {
		color: #4b5eeb;
	}

	.order_detils {
		padding: 30rpx;
	}

	.order {
		// width: 100%;
		// height: 200rpx;
		margin: 20rpx;
		position: relative;
		border-radius: 30rpx;
		background-color: #ffffff;
		padding: 15rpx;
		box-sizing: border-box;
		box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
		// margin-bottom: 25rpx;

		.order-number {
			width: 100%;
			color: #c73614;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 2rpx solid #f3f3f3;
			padding-bottom: 20rpx;

			.number-left {
				display: flex;
				align-items: center;
				font-weight: bold;

				image {
					width: 30rpx;
					height: 30rpx;
					margin-right: 15rpx;
				}

				text {
					font-size: 30rpx;
					letter-spacing: 3rpx;
				}
			}

			.number-right {
				font-size: 26rpx;
				font-weight: bold;
				letter-spacing: 3rpx;
				padding-right: 5rpx;
				box-sizing: border-box;
			}

		}

		.order-item {
			width: 100%;
			display: flex;
			align-items: center;
			height: 50rpx;

			image {
				width: 30rpx;
				height: 30rpx;
				margin-right: 15rpx;
			}

			text {
				font-size: 24rpx;
				color: rgba(0, 0, 0, 0.8);
			}
		}

		.order-price {
			position: absolute;
			top: 38%;
			right: 4%;
			font-size: 50rpx;
			font-weight: 700;
			color: red;
		}

		.order-bom {
			width: 100%;
			height: 60rpx;
			background-color: #f5f5f5;
			display: flex;
			align-items: center;
			justify-content: space-around;

			.order-bom-left {
				color: rgba(0, 0, 0, 0.5);
				font-size: 24rpx;
				padding-left: 40rpx;
				box-sizing: border-box;
			}

			.order-bom-right {
				// width: 200rpx;
				height: 100%;
				display: flex;
				color: #ffffff;
				font-size: 26rpx;

				.box1 {
					height: 100%;
					line-height: 60rpx;
					background-color: #e5a84a;
					padding: 0 40rpx;
					box-sizing: border-box;
					border-radius: 10rpx;
					margin-right: 10rpx;
				}

				.box2 {
					height: 100%;
					line-height: 60rpx;
					background-color: #56caa3;
					padding: 0 40rpx;
					box-sizing: border-box;
					border-radius: 10rpx;
					margin-right: 10rpx;
				}

				.box3 {
					height: 100%;
					line-height: 60rpx;
					background-color: #526afc;
					padding: 0 30rpx;
					box-sizing: border-box;
					border-radius: 10rpx;
					margin-right: 10rpx;
				}
			}
		}
	}
</style>